package com.sas.repositoryImpl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.sas.domain.Client;
import com.sas.repository.ClientRepository;

@Repository
public class HibernateClientRepository extends 
HibernateGenericRepository<Client> implements ClientRepository {
	
	@Autowired
	public HibernateClientRepository (SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
	}


	@SuppressWarnings("unchecked")
	@Override
	public boolean exists(Client client) {
		Session session = getSession();
		Query query = session.createQuery("from Client where cuilCuit = ?");
		query.setString(0, client.getCuilCuit());
		List<Client> ret = query.list();
		return ret.size() != 0? true: false;
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<Client> findByProperty(String property, boolean ascending) {
		Session session = getSession();
		
		String ascendent = ascending ? " asc" : " desc";
		String sort = "order by  c." + property + " " + ascendent;
		Query query = session.createQuery("select c from Client c "+ sort);
		return query.list();
	}

}
